﻿using BitBenderGames;
using System;
using UnityEngine;

public class CommanderScene : MonoBehaviour
{
    // Start is called before the first frame update
   // private string[] CommanderLayers = { "commander1", "commander2" };

    private CommanderMobileTouchCamera _cameraCtrl;

    private Action<int> _scrollFunc;
    private Action _pickPressedFunc;
    private Action _moveStartFunc;
    private Action _moveEndFunc;
    private Action _moveInEndFunc;
    public static Vector3 playerScreenPoint
    {
        get;set;
    }

    //void Start()
    //{
    //    //ResourcesManager.GetInstance();
    //    //_cameraCtrl = find
    //}

    //// Update is called once per frame
    //void Update()
    //{
        
    //}

    public void SetLuaFunc(Action<int> func, Action moveStart, Action moveEnd, Action clicked, Action moveInEnd)
    {
        _scrollFunc = func;
        _moveStartFunc = moveStart;
        _moveEndFunc = moveEnd;
        _pickPressedFunc = clicked;
        _moveInEndFunc = moveInEnd;
    }

    public void OnPickPressed(Transform tras)
    {
        if (_pickPressedFunc != null)
        {
            _pickPressedFunc();
        }
    }

    public void OnMoveInEnd()
    {
        if (_moveInEndFunc != null)
        {
            _moveInEndFunc();
        }
    }

    public void OnScrolling(int isRight)
    {
        if (_scrollFunc != null)
        {
            _scrollFunc(isRight);
        }
    }

    public void OnScrollStart()
    {
        if (_moveStartFunc != null)
        {
            _moveStartFunc();
        }
    }
    public void OnScrollEnd()
    {
        if (_moveEndFunc != null)
        {
            _moveEndFunc();
        }
    }

    //private void OnGUI()
    //{
    //    if(GUI.Button(new Rect(10,10,50,50),"设置layer"))
    //    {
    //        foreach (Transform tran in GetComponentsInChildren<Transform>())
    //        {//遍历当前物体及其所有子物体
    //            tran.gameObject.layer = LayerMask.NameToLayer(CommanderLayers[0]);//更改物体的Layer层
    //        }
    //        string tmp = CommanderLayers[1];
    //        CommanderLayers[1] = CommanderLayers[0];
    //        CommanderLayers[0] = tmp;

    //    }

    //    if (GUI.Button(new Rect(60, 10, 50, 50), "加载scene"))
    //    {
    //        ResourcesManager.ReadAsync(this.gameObject, "Assets/ResourcesAssets/Prefabs/Scene/TestCommanderScene.prefab", (obj, r) =>
    //        {
    //            if (obj == null)
    //            {
    //                UnityEngine.Debug.LogError("dont has this spriteatlas:");
    //            }
    //            else
    //            {
    //                //var spriteatlas = obj as SpriteAtlas;
    //                //_target.sprite = spriteatlas.GetSprite(spritename);
    //                //_target.gameObject.SetActive(true);
    //                var scene = obj as GameObject;
    //                scene.transform.SetParent(this.transform);
    //                scene.transform.localPosition = Vector3.zero;
    //            }
    //        }, false);
    //        UpdateLayer();

    //    }
    //}

    //public void UpdateLayer()
    //{
    //    foreach (Transform tran in GetComponentsInChildren<Transform>())
    //    {//遍历当前物体及其所有子物体
    //        tran.gameObject.layer = LayerMask.NameToLayer(CommanderLayers[0]);//更改物体的Layer层
    //    }
    //    string tmp = CommanderLayers[1];
    //    CommanderLayers[1] = CommanderLayers[0];
    //    CommanderLayers[0] = tmp;
    //}

    /// <summary>
    /// 
    /// </summary>
    /// <param name="layerName"></param>
    /// <param name="obj"></param>
    /// <param name="moveLimit">移动限制：0无限制  1不能往左边滑了  2不能往右边滑了</param>
    /// <param name="bornSide">0默认位置1从左边进2从右边进</param>
    public void SceneLoaded(string layerName, GameObject obj, int moveLimit, int bornSide = 0)
    {
        UpdateLayer(layerName, obj);
        Camera ca = obj.GetComponentInChildren<Camera>();
        ca.cullingMask = 1 << LayerMask.NameToLayer(layerName);
        _cameraCtrl = ca.transform.GetComponent<CommanderMobileTouchCamera>();
        _cameraCtrl.SetScrollCallback(OnScrolling, OnScrollStart, OnScrollEnd);
        _cameraCtrl.SetClickCallback(OnPickPressed);
        _cameraCtrl.moveLimit = moveLimit;
        if (bornSide > 0)
        {
            //StartCoroutine(MoveIn(bornSide));
            _cameraCtrl.MoveIn(bornSide, OnMoveInEnd);
        }
    }

    public void MoveToTargetPosition(Vector3 screenPoint)
    {
        if(_cameraCtrl)
        {
            _cameraCtrl.MoveToTargetPosition(screenPoint);
        }
    }

    public void MoveBack()
    {
        if(_cameraCtrl)
        {
            _cameraCtrl.MoveBack();
        }
    }

    public void MoveOut(GameObject obj, int bornSide)
    {
        Camera ca = obj.GetComponentInChildren<Camera>();
        CommanderMobileTouchCamera ctrl = ca.transform.GetComponent<CommanderMobileTouchCamera>();
        ctrl.MoveOut(bornSide);
    }

    //public void Move

    //private IEnumerator MoveIn(int bornSide)
    //{
    //    yield return new WaitForEndOfFrame();
    //    _cameraCtrl.MoveIn(bornSide);
    //}


    private void UpdateLayer(string LayerName, GameObject obj)
    {
        //遍历当前物体及其所有子物体
        foreach (Transform tran in obj.GetComponentsInChildren<Transform>(true))
            tran.gameObject.layer = LayerMask.NameToLayer(LayerName); //更改物体的Layer层
        //string tmp = CommanderLayers[1];
        //CommanderLayers[1] = CommanderLayers[0];
        //CommanderLayers[0] = tmp;
    }
}
